package com.javaclimb.drug.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.javaclimb.drug.entity.Prescription;
import com.javaclimb.drug.mapper.PrescriptionMapper;
import com.javaclimb.drug.service.IPrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处方service实现类
 */
@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements IPrescriptionService {

    @Autowired
    private PrescriptionMapper prescriptionMapper;

    /**
     * 分页查询处方数据
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条数据
     * @param param    查询参数-处方名称
     * @return
     */
    @Override
    public IPage<Prescription> selectPrescriptionPage(int pageNum, int pageSize, String param) {
        QueryWrapper<Prescription> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(param)){
            queryWrapper.eq("name",param);
        }
        Page<Prescription> page = new Page<>(pageNum,pageSize);
        return prescriptionMapper.selectPage(page,queryWrapper);
    }

    /**
     * 新增一条处方信息
     *
     * @param prescription
     */
    @Override
    public int addPrescription(Prescription prescription) {
        return prescriptionMapper.insert(prescription);
    }

    /**
     * 修改一条处方信息
     *
     * @param prescription
     */
    @Override
    public int editPrescription(Prescription prescription) {
        return prescriptionMapper.updateById(prescription);
    }

    /**
     * 根据主键id查询一个处方对象
     *
     * @param id
     * @return
     */
    @Override
    public Prescription queryPrescriptionById(Integer id) {
        return prescriptionMapper.selectById(id);
    }

    /**
     * 根据主键id删除一个处方对象
     *
     * @param id
     * @return
     */
    @Override
    public int delPrescriptionById(Integer id) {
        return prescriptionMapper.deleteById(id);
    }

}
